#include "../include/WebPage.h"
#include <regex>
#include <algorithm>
#include <cctype>

using std::regex;
using std::smatch;
using std::stoi;
using std::string;

size_t getByteNum_UTF8(const char byte)
{
    int byteNum = 0;
    for (size_t i = 0; i < 6; ++i)
    {
        if (byte & (1 << (7 - i)))
            ++byteNum;
        else
            break;
    }
    return byteNum == 0 ? 1 : byteNum;
}

// 构造函数的实现
WebPage::WebPage() {}
WebPage::WebPage(const vector<string> &queryWords, const std::string &doc)
{
    processDoc(queryWords, doc);
}

// 获取文档的 docid 的实现
int WebPage::getDocId() const
{
    return _docId;
}

// 获取文档的标题的实现
string WebPage::getTitle() const
{
    return _docTitle;
}

// 获取文档的url
string WebPage::getURL() const
{
    return _docUrl;
}

// 获取文档内容
string WebPage::getContent() const
{
    return _docContent;
}

// 获取文档摘要
string WebPage::getSummary() const
{
    return _docSummary;
}
// 判断一个字符是否是有效的 UTF-8 字节
bool isValidUTF8Byte(unsigned char c)
{
    return (c & 0xC0) == 0x80;
}

// 对格式化文档进行处理的实现
void WebPage::processDoc(const vector<string> &queryWords, const string &doc)
{
    regex idPattern("<docid>(\\d+)</docid>");
    regex titlePattern("<title>(.+)</title>");
    regex urlPattern("<link>(.+)</link>");
    regex contentPattern("<content>(.{0,20000})</content>");
    regex descriptionPattern("<description>(.{0,20000})</description>");

    smatch matches;

    if (regex_search(doc, matches, idPattern))
    {
        _docId = stoi(matches[1].str());
    }
    else
    {
        _docId = 0;
    }

    if (regex_search(doc, matches, titlePattern))
    {
        _docTitle = matches[1].str();
    }
    else
    {
        _docTitle = "";
    }

    if (regex_search(doc, matches, urlPattern))
    {
        _docUrl = matches[1].str();
    }
    else
    {
        _docUrl = "";
    }

    if (regex_search(doc, matches, contentPattern))
    {
        if (!matches.empty())
        {
            _docContent = matches[1].str();
        }
        else
        {
            _docContent = "";
        }
    }
    else
    {
        _docContent = "";
    }

    if (regex_search(doc, matches, descriptionPattern))
    {
        if (!matches.empty())
        {
            _docSummary = matches[1].str();
        }
        else
        {
            _docSummary = "";
        }
    }
    else
    {
        _docSummary = "";
    }

    // 提取摘要并标红
    string strTemp = _docContent + _docSummary;
    if (strTemp.size() == 0)
    {
        return;
    }
    _docSummary = "";

    for (const auto &elem : queryWords)
    {
        size_t pos = strTemp.find(elem);

        int number = 3;
        while (pos != std::string::npos && pos + 45 < strTemp.length() && number--)
        {

            int count = 15;
            size_t num = 0;
            while (count--)
            {
                num += getByteNum_UTF8(strTemp[pos + num]);
            }
            _docSummary += strTemp.substr(pos, num);
            _docSummary += " ";

            pos = strTemp.find(elem, pos + elem.length());
        }
    }

    // 去除无效的utf-8字符

    for (const auto &elem : queryWords)
    {
        string relapcement = "\033[31m" + elem + "\033[0m";
        size_t pos = _docSummary.find(elem);
        while (pos != std::string::npos)
        {
            _docSummary.replace(pos, elem.length(), relapcement);
            pos = _docSummary.find(elem, pos + relapcement.length());
        }
    }
}
